const app = getApp()
const bus = require('../utils/bus')
const db = wx.cloud.database()


Component({
  options: {
    addGlobalClass: true,
  },
  lifetimes: {

  },

  pageLifetimes: {

  },

  data: {
    selected: null,
    modalStatus: false,
    authorizeModal: false,
    "list": [{
        pagePath: "/pages/index/index",
        iconPath: "/image/tabbar/home.png",
        selectedIconPath: "/image/tabbar/home_cur.png",
        text: "首页"
      },
      {
        pagePath: "/pages/square/square",
        iconPath: "/image/tabbar/square.png",
        selectedIconPath: "/image/tabbar/square_cur.png",
        text: "广场"
      },
      {
        text: "发布",
        isSpecial: true
      },
      {
        pagePath: "/pages/chat/chat",
        iconPath: "/image/tabbar/msg.png",
        selectedIconPath: "/image/tabbar/msg_cur.png",
        text: "消息"
      },
      {
        pagePath: "/pages/my/my",
        iconPath: "/image/tabbar/my.png",
        selectedIconPath: "/image/tabbar/my_cur.png",
        text: "我的"
      }
    ]
  },

  methods: {
    switchTab(e) {
      console.log(e.currentTarget.dataset)
      const url = e.currentTarget.dataset.path

      // 点击中间特殊的按钮,注意dataset都会被转化为小写
      if (e.currentTarget.dataset.isspecial) {
        this.setData({
          modalStatus: !this.data.modalStatus
        })

        console.log('bus', bus)
        bus.on('indexModalEvent', (params) => {
          console.log('params', params)
          this.setData({
            modalStatus: params
          })
        })
        console.log('bus', bus)
      } else {
        wx.switchTab({
          url
        })
      }
    },

    hiddenModal() {
      // 触发app.js中的getter和setter
      this.setData({
        modalStatus: !this.data.modalStatus
      })
      bus.off('indexModalEvent')
    },

    onPublishSecondHand() {
      let userInfo = wx.getStorageSync('userInfo')
      if (!userInfo) {
        this.setData({
          authorizeModal: true
        })
        return
      }
      wx.navigateTo({
        url: '/pages/publish-items/secondhand/secondhand'
      })
    },

    onPublishLost() {
      // wx.navigateTo({
      //   url: '/pages/publish-items/lost/lost'
      // })
      wx.showToast({
        title: '开发中...',
        icon:'loading'
      })
    },

    onPackage(){
      wx.showToast({
        title: '开发中...',
        icon:'loading'
      })
    },

    onSchoolActivity(){
      wx.showToast({
        title: '开发中...',
        icon:'loading'
      })
    },

    authorizeLogin(e) {
      console.log('e.detail.userInfo', e.detail.userInfo)
      if (e.detail.userInfo) {
        this.setData({
          authorizeModal: false
        })
        wx.setStorageSync('userInfo', e.detail.userInfo)
        app.globalData.userInfo = e.detail.userInfo
        this.addUser(app.globalData.userInfo)
      }
    },

    // 如果数据库没有此用户，则添加
    async addUser(user) {
      try {
        // 检查数据库是否有了
        let res = await db.collection('user').where({
          _openid: wx.getStorageSync('openid')
        }).get()
        if (res.data.length === 0) {
          console.log('该用户为新用户')
          // 没有的话添加
          await db.collection('user').add({
            data: user
          })
          wx.showToast({
            title: '登陆成功',
          })
        } else {
          await db.collection('user').where({
            _openid: wx.getStorageSync('openid')
          }).update({
            data: user
          })
        }
      } catch (e) {
        wx.showToast({
          title: '啊哦，出错了',
        })
      }
    },

    hideAuthorizeModal() {
      this.setData({
        authorizeModal: false
      })
    }
  }
})